package com.xpq.petserver.exception;

import com.xpq.petserver.entity.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理器
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理所有抛出的异常
     * @param e
     * @param <T>
     * @return
     */
    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public <T> R exceptionHandler(Exception e) {
        // 处理自己的业务异常
        if (e instanceof AppException) {
            AppException appException = (AppException) e;
            return R.error(appException.getCode(), appException.getMsg());
        }
        e.printStackTrace();
        return R.error(500, "服务器异常");
    }
}
